package com.es;

import com.es.dto.Product;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.List;

public class TransportClientDemo {

    public static void main(String[] args) throws Exception {
//        create();
//        get();
//        getAll();
//        update();
//        delete();

        multiSearch();
    }

    public static List<Product> buildProducts(){

//        Long id, String name, String desc, int price, String tags, String date
        Product p1 = new Product(1, "小米手机", "为发烧而生", 3999, Arrays.asList("性价比","续航","便宜"), "2024-11-13 17:09:10");
        Product p2 = new Product(2, "小米手表", "比小天才更好的电话手表", 299, Arrays.asList("性价比","小天才","打电话"), "2024-11-12 17:09:10");
        Product p3 = new Product(3, "小米电视", "不要四个九，不要四个八", 6999, Arrays.asList("超薄","视觉体验","高清"), "2024-11-15 17:09:10");
        Product p4 = new Product(4, "小米汽车", "小米汽车，为不加油而生", 6999, Arrays.asList("快","稳","安全"), "2024-11-12 17:09:10");
        List<Product> datas = Arrays.asList(p1, p2, p3, p4);
        return datas;
    }

    public static void create() throws IOException {
        TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

        List<Product> datas = buildProducts();

        for(Product p:datas) {

            IndexResponse response = client.prepareIndex("product", "_doc", String.valueOf(p.getId()))
                    .setSource(XContentFactory.jsonBuilder()
                            .startObject()
                            .field("name", p.getName())
                            .field("desc", p.getDesc())
                            .field("price", p.getPrice())
                            .field("date", p.getDate())
                            .field("tags", p.getTags())
                            .endObject()
                    )
                    .get();
            System.out.println(response);
        }

        client.close();
    }

    public static void get() throws Exception{
        TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

        GetResponse response = client.prepareGet("product", "_doc", "1").get();
        System.out.println(response);

        client.close();
    }

    public static void getAll() throws Exception{
        TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

        SearchResponse response = client.prepareSearch("product").get();
        System.out.println(response);

        client.close();
    }

    public static void update() throws Exception {
        TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

        UpdateResponse response = client.prepareUpdate("product", "_doc", "1").setDoc(XContentFactory.jsonBuilder()
                .startObject()
                .field("name", "红米手机")
                .endObject()).get();

        System.out.println(response);

        client.close();
    }

    public static void delete() throws Exception {
        TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

        DeleteResponse response = client.prepareDelete("product", "_doc", "1").get();

        System.out.println(response);

        client.close();
    }

    public static void multiSearch() throws Exception{
        TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

        SearchRequestBuilder response = client.prepareSearch("product")
                .setQuery(QueryBuilders.boolQuery()
                        .must(QueryBuilders.matchQuery("name", "小米"))
                        .must(QueryBuilders.rangeQuery("price").from(299).to(3999))
                );

        System.out.println(response);

        client.close();
    }
    
}
